package com.hzdesk.media.koreabatchmatvector.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.bean.XmlUserBean;

import java.util.List;

/**
 * Created by Administrator on 2018/7/26.
 */

public class VideoPlayerSetAdapter extends RecyclerView.Adapter{
    private String TAG="VideoPlayerSetAdapter";
    private List<XmlUserBean> mList;
    private Context mContext;
    private OnSwitchChangeLisenter onSwitchChangeLisenter;
    private boolean isAllShow=true;
    private boolean isAllHide=true;

    public VideoPlayerSetAdapter(List<XmlUserBean> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    public void setOnSwitchChangeLisenter(OnSwitchChangeLisenter onSwitchChangeLisenter) {
        this.onSwitchChangeLisenter = onSwitchChangeLisenter;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.video_play_set_item,parent,false);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MyViewHolder){
            final MyViewHolder myViewHolder= (MyViewHolder) holder;
            if (position!=mList.size()){
                final XmlUserBean xmlUserBean=mList.get(position);
                myViewHolder.textView_user.setText(xmlUserBean.getName());
                myViewHolder.view_line.setVisibility(View.GONE);
                if (xmlUserBean.getIsShow()==1){
                    myViewHolder.imageView.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.video_player_canvas_open));
                }else {
                    myViewHolder.imageView.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.video_player_canvas_close));
                }

                myViewHolder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (xmlUserBean.getIsShow()==1){
                            mList.get(position).setIsShow(0);
                            if (onSwitchChangeLisenter!=null){
                                onSwitchChangeLisenter.hide(xmlUserBean.getNumber());
                            }
                        }else {
                            mList.get(position).setIsShow(1);
                            if (onSwitchChangeLisenter!=null){
                                onSwitchChangeLisenter.show(xmlUserBean.getNumber());
                            }
                        }
                        myViewHolder.imageView.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(),xmlUserBean.getIsShow()==1? R.drawable.video_player_canvas_open:R.drawable.video_player_canvas_close));
                        notifyDataSetChanged();
                    }
                });
            }else {
                isAllHide=true;
                isAllShow=true;
                for (XmlUserBean xmlUserBean1:mList){
                    if (xmlUserBean1.getIsShow()==1){
                        isAllHide=false;
                    }
                    if (xmlUserBean1.getIsShow()==0){
                        isAllShow=false;
                    }
                }
                myViewHolder.view_line.setVisibility(View.VISIBLE);
                myViewHolder.textView_user.setText(R.string.display_all_the_postil_mark);
                if (isAllShow){
                    myViewHolder.imageView.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.video_player_canvas_open));
                }else {
                    myViewHolder.imageView.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.video_player_canvas_close));
                }

                myViewHolder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isAllShow){
                            hideAll();
                        }
                        else {
                            showAll();
                        }
                        myViewHolder.imageView.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(),isAllShow? R.drawable.video_player_canvas_open:R.drawable.video_player_canvas_close));
                        notifyDataSetChanged();

                    }

                });

            }


        }

    }

    public void setDate(List<XmlUserBean> list){
        mList=list;
        notifyDataSetChanged();
    }

    /**
     * 隐藏所有
     */
    private void hideAll() {
        for (int i=0;i<mList.size();i++){
            mList.get(i).setIsShow(0);
        }
        onSwitchChangeLisenter.allhide();
        isAllShow=false;
        isAllHide=true;

    }

    /**
     * 显示所有
     */
    private void showAll() {
         for (int i=0;i<mList.size();i++){
            mList.get(i).setIsShow(1);
        }
        onSwitchChangeLisenter.allShow();
        isAllShow=true;
        isAllHide=false;
    }

    @Override
    public int getItemCount() {
        return mList.size()+1;
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView textView_user;
        public ImageView imageView;
        public View view_line;

        public MyViewHolder(View itemView) {
            super(itemView);
            textView_user=itemView.findViewById(R.id.video_play_set_item_tv);
            imageView=itemView.findViewById(R.id.video_play_set_item_iv);
            view_line=itemView.findViewById(R.id.video_play_set_item_view);
        }
    }

    public interface OnSwitchChangeLisenter{
        void  show(int num);
        void  hide(int num);
        void  allShow();
        void  allhide();
    }

}
